package org.crmsystem.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.crmsystem.dao.RoleMapper;
import org.crmsystem.dao.UserMapper;
import org.crmsystem.model.Role;
import org.crmsystem.model.User;
import org.crmsystem.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * 角色服务层实现
 * @author 孙艳平
 *
 */
@Service
public class RoleServiceImpl implements IRoleService {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private UserMapper userMapper;
	/**
	 * 根据id获取角色详细信息
	 */
	public Role getRoleById(int id) {
		return roleMapper.getRoleById(id);
	}

	/**
	 * 多条件查询获取角色列表
	 */
	public List<Role> getRole(Role role) {
		return roleMapper.getRole(role);
	}

	/**
	 * 添加角色
	 */
	public int addRole(Role role) {
		return roleMapper.addRole(role);
	}
	/**
	 * 删除角色
	 */
	@Transactional
	public int delRole(int id) {
		User user=new User();
    	user.setRoleId(id);
    	Map map=new HashMap();
    	map.put("user", user);
    	map.put("currPage", 0);
    	map.put("pageSize", 0);
    	List<Object> list=userMapper.getUser(map);
    	for(int i=0;i<list.size();i++){
    		User user1=(User)list.get(i);
    		user1.setRoleId(-1);
    		user1.setUseState("禁用");
    		userMapper.updateUser(user1);
    	}
    	roleMapper.updateRolePermission(id);
        return roleMapper.delRole(id);
	}
	
	/**
	 * 更新角色
	 */
	public int updateRole(Role role) {
		return roleMapper.updateRole(role);
	}

}